package main

import "fmt"

// Point2D 平面坐标
type Point2D struct {
	x, y int
}

func newPoint(x, y int) *Point2D {
	return &Point2D{x, y}
}

// 针对Point2D特有的方法
func (p *Point2D) print() {
	// p 类似于其他语言中的this指针
	fmt.Printf("x=%d, y=%d\n", p.x, p.y)
}

func (p Point2D) move() Point2D {
	p.x++
	return p
}

func (p *Point2D) move2() {
	p.x++
}

type myInt int

func (m myInt) eq(n myInt) bool {
	return m == n
}

type persion struct {
	string
	int
}

type persion2 struct {
	name string
	age  int
	com  company
}
type persion3 struct {
	name string
	age  int
	company
}

type company struct {
	name string
	addr string
}

func main() {
	var p1 = newPoint(1, 2)
	// var p1 = &Point2D{1, 2}
	fmt.Println(p1)
	p1.print() // x=1, y= 2

	var p2 = newPoint(0, 0)
	p2.move2()
	fmt.Println(p2) // &{1 0}
	p2.move()       // 语法糖会自动转换成  (*p2).move()
	fmt.Println(p2) // &{1 0}
	var p3 = Point2D{0, 0}
	p3.move()
	fmt.Println(p3) // {0 0}
	p3.move2()      //语法糖会自动转换成 (&p3).move2()
	fmt.Println(p3) // {1 0}

	var num1, num2 = myInt(100), myInt(200)
	fmt.Println(num1.eq(num2)) // false

	var p6 = persion{"ming", 35}
	fmt.Println(p6, p6.int, p6.string) // {ming 35} 35 ming

	var p7 = persion2{"wang", 22, company{"google", "xx-xx"}}
	fmt.Println(p7, p7.com.name) // {wang 22 {google xx-xx}} google
	// 匿名嵌套
	var p8 = persion3{"wang", 22, company{"google", "xx-xx"}}
	fmt.Println(p8.company.name, p8.name, p8.addr) // google wang xx-xx
	// p8.addr => p8.company.addr

}
